<template>
  <a-button @click="show">显示弹框</a-button>
  <teleport to="body">
    <transition name="slide-fade2">
      <div
        v-show="visibleModal"
        class="modal-box-mask"
        @click="close"
      />
    </transition>
    <transition name="slide-fade">
      <div
        v-show="visibleModal"
        class="modal-box"
        :style="{width: width + 'px'}"
      >
        <div class="modal-head">
          <div class="title">
            <!--具名插槽内容优先级会高于 props-->
            <slot name="title">
              <span>{{ title }}</span>
            </slot>
          </div>
          <CloseOutlined @click="close"/>
        </div>
        <div class="modal-body">
          <slot/>
        </div>
      </div>
    </transition>
  </teleport>
</template>

<script lang="ts">
import { ref, ComponentPropsOptions } from 'vue'
import { CloseOutlined } from '@ant-design/icons-vue'
import { SetupContext } from '@vue/runtime-core'

export default {
  name: 'TeleportModal',
  components: { CloseOutlined },
  emits: {
    toggle: (status: any) => {
      if (typeof status === 'boolean') {
        return true
      } else {
        console.warn('toggle 事件校验不通过！')
        return false
      }
    }
  },
  props: {
    // 模态框的宽度
    width: {
      type: Number,
      default: 500
    },
    // 模态框的标题（也可以用具名插槽）
    title: {
      type: String,
      default: '标题'
    }
  },
  setup (props: ComponentPropsOptions, { emit }: SetupContext) {
    const visibleModal = ref(false)

    function close () {
      visibleModal.value = false
      emit('toggle', false)
    }

    function show () {
      visibleModal.value = true
      emit('toggle', true)
    }

    return {
      visibleModal,
      close,
      show
    }
  }
}
</script>

<style scoped lang="less">
.modal-box-mask {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 1000;
  height: 100%;
  background-color: rgba(0, 0, 0, .45);
}

.modal-box {
  position: absolute;
  top: 40%;
  left: 50%;
  z-index: 1000;
  transform: translate(-50%, -50%);
  background: #fff;
  border-radius: 2px;
  box-shadow: 0 0 8px #999;

  .modal-body {
    min-height: 160px;
    padding: 24px;
    font-size: 14px;
    line-height: 1.5715;
    word-wrap: break-word;
  }

  .modal-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 24px;
    color: rgba(0, 0, 0, .85);
    border-bottom: 1px solid #f0f0f0;
    border-radius: 2px 2px 0 0;
  }
}

.slide-fade-enter-active {
  transition: all 0.3s ease-out;
}

.slide-fade-leave-active {
  transition: all 0.3s cubic-bezier(1, 0.5, 0.8, 1);
}

.slide-fade-enter-from,
.slide-fade-leave-to {
  transform: translate(-50%, -50%) scale3d(0, 0, 0);
  opacity: 0;
}

//--------------------------分隔符--------------------------
.slide-fade2-enter-active {
  transition: all 0.3s ease-out;
}

.slide-fade2-leave-active {
  transition: all 0.3s cubic-bezier(1, 0.5, 0.8, 1);
}

.slide-fade2-enter-from,
.slide-fade2-leave-to {
  opacity: 0;
}
</style>
